// Write your cade below:
const readlindSync = require('readline-sync');//输入模块，你要安装的

function main(){
    menu();
    let choice = readlindSync.question();
    if(choice === '1'){
        let random = randomString();//数字字符
        console.log("************随机数已生成**************")
        let younumber = numberEnterandCheck_returnString();//数字字符
        let result = younumberTorandom(younumber,random);
        while(result !== '4A0B'){
            console.log("------------------")
            console.log(result);
            console.log('不对哦！加油哦！');
            younumber = numberEnterandCheck_returnString();
            result = younumberTorandom(younumber,random);
        }
        console.log("YOU WIN！");
    }else{
        console.log('GAME OVER!');
    }
    return 0 ;
}
//menu
function menu(){
    console.log('************小竹子，猜数字************');
    console.log('***************请选择*****************');
    console.log('1.play\t2.exit');
}
//输入 检测 
function numberEnterandCheck_returnString() {
    console.log('Enter a 4-digit integer, similar to 1234');
    let num = readlindSync.question();
    while(num.toString().length !== 4){
        num = readlindSync.question(`Please re-enter, enter a 4-digit integer, similar to 1234:\t`);
    }
    return num ;
}
//产生min到max之间的随机整数
function getRandomInt(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
  }
//产生不重复的随机数
function randomString(){
    let random = getRandomInt(1000,9999);
    random = random.toString();
    for(let index = 0;index < 4 ;index ++){
        if(random.lastIndexOf(random[index]) !== index){
            random = randomString();
        }
    }
    return random;
}
//比较输出,这个好像才是任务卡的部分。。。。
//传入的是两个4位数字字符串
function younumberTorandom(younumber,random){
    let result = {
        'A':0,
        'B':0
    }
    let younumberLength = younumber.length;
    for(let index = 0;index < younumberLength;index++){
        let value = younumber[index];
        if(random.indexOf(value) !== -1){
            if(random.indexOf(value) === index){
                result['A']++;
            }else{
                result['B']++;
            }
        }
    }
    let resultString = `${result['A']}A${result['B']}B`;
    return resultString ;
}
module.exports = {
    younumberTorandom
}
 /**                                                                      
 *            .,,       .,:;;iiiiiiiii;;:,,.     .,,                     
 *          rGB##HS,.;iirrrrriiiiiiiiiirrrrri;,s&##MAS,                  
 *         r5s;:r3AH5iiiii;;;;;;;;;;;;;;;;iiirXHGSsiih1,                 
 *            .;i;;s91;;;;;;::::::::::::;;;;iS5;;;ii:                    
 *          :rsriii;;r::::::::::::::::::::::;;,;;iiirsi,                 
 *       .,iri;;::::;;;;;;::,,,,,,,,,,,,,..,,;;;;;;;;iiri,,.             
 *    ,9BM&,            .,:;;:,,,,,,,,,,,hXA8:            ..,,,.         
 *   ,;&@@#r:;;;;;::::,,.   ,r,,,,,,,,,,iA@@@s,,:::;;;::,,.   .;.        
 *    :ih1iii;;;;;::::;;;;;;;:,,,,,,,,,,;i55r;;;;;;;;;iiirrrr,..         
 *   .ir;;iiiiiiiiii;;;;::::::,,,,,,,:::::,,:;;;iiiiiiiiiiiiri           
 *   iriiiiiiiiiiiiiiii;;;::::::::::::::::;;;iiiiiiiiiiiiiiiir;          
 *  ,riii;;;;;;;;;;;;;:::::::::::::::::::::::;;;;;;;;;;;;;;iiir.         
 *  iri;;;::::,,,,,,,,,,:::::::::::::::::::::::::,::,,::::;;iir:         
 * .rii;;::::,,,,,,,,,,,,:::::::::::::::::,,,,,,,,,,,,,::::;;iri         
 * ,rii;;;::,,,,,,,,,,,,,:::::::::::,:::::,,,,,,,,,,,,,:::;;;iir.        
 * ,rii;;i::,,,,,,,,,,,,,:::::::::::::::::,,,,,,,,,,,,,,::i;;iir.        
 * ,rii;;r::,,,,,,,,,,,,,:,:::::,:,:::::::,,,,,,,,,,,,,::;r;;iir.        
 * .rii;;rr,:,,,,,,,,,,,,,,:::::::::::::::,,,,,,,,,,,,,:,si;;iri         
 *  ;rii;:1i,,,,,,,,,,,,,,,,,,:::::::::,,,,,,,,,,,,,,,:,ss:;iir:         
 *  .rii;;;5r,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,sh:;;iri          
 *   ;rii;:;51,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.:hh:;;iir,          
 *    irii;::hSr,.,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,.,sSs:;;iir:           
 *     irii;;:iSSs:.,,,,,,,,,,,,,,,,,,,,,,,,,,,..:135;:;;iir:            
 *      ;rii;;:,r535r:...,,,,,,,,,,,,,,,,,,..,;sS35i,;;iirr:             
 *       :rrii;;:,;1S3Shs;:,............,:is533Ss:,;;;iiri,              
 *        .;rrii;;;:,;rhS393S55hh11hh5S3393Shr:,:;;;iirr:                
 *          .;rriii;;;::,:;is1h555555h1si;:,::;;;iirri:.                 
 *            .:irrrii;;;;;:::,,,,,,,,:::;;;;iiirrr;,                    
 *               .:irrrriiiiii;;;;;;;;iiiiiirrrr;,.                      
 *                  .,:;iirrrrrrrrrrrrrrrrri;:.                          
 *                        ..,:::;;;;:::,,.                               
 */